package l1j.opqlo.NewSystem;

import java.util.ArrayList;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.List.特殊列表;
import l1j.opqlo.NewSystem.Extension.TreasureMaster;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.utils.Random;


public class goblinThread  extends TimerTask {

	private final static Logger _log = Logger.getLogger(goblinThread.class.getName());


	private static int minus = 0;
	private static int chance = 0;
	
	private static goblinThread _instance;
	
	public static goblinThread getInstance(){
		
		if(_instance == null){
			_instance = new goblinThread();
		}
		return _instance;
	}


	private ScheduledFuture<?> _timer;
	
	public void start(){
		_timer = GeneralThreadPool.getInstance().scheduleAtFixedRate(this, 60000, 60000);
		chance = Random.nextInt(40)+30;
		_allMap.add(604);
		_allMap.add(605);
		_allMap.add(606);
		_allMap.add(607);
		_allMap.add(6321);
		_allMap.add(9901);
		_allMap.add(9914);
		_allMap.add(9915);
		_allMap.add(9916);
		_allMap.add(9918);
		_allMap.add(9919);
		_allMap.add(9920);
		_allMap.add(9921);
		_allMap.add(9922);
		_allMap.add(9923);
		_allMap.add(9991);
		_allMap.add(9992);
		_allMap.add(9993);
		_allMap.add(9994);
		_allMap.add(9995);
	}

	private ArrayList<Integer>_tempList = new ArrayList<Integer>();
	private ArrayList<Integer>_allMap = new ArrayList<Integer>();
	private ConcurrentHashMap<Integer , L1NpcInstance>_mobList = new ConcurrentHashMap<Integer , L1NpcInstance>();

	@Override
	public void run() {
		try{
			minus++;
			if(minus >=chance){
				final int count = 10 - _mobList.size();
				while(_tempList.size() < count){
					final int rate = Random.nextInt(_allMap.size());
					final int mapId = _allMap.get(rate);
					if(!_mobList.containsKey(mapId)){
					_tempList.add(mapId);
					_allMap.remove(rate);
					}
				}
				for(int i= _tempList.size()-1 ; i>=0; i--){
					final int mapId = _tempList.get(i);
					L1NpcInstance[] mob =TreasureMaster.getInstance().spawn(0, 2000263, 1, 0,0, (short) mapId, 1, 0, false);
					Thread.sleep(500);
					L1World.getInstance().broadcastPacketToAll("\\aL[噴錢]盜寶魔 出現在【 "+特殊列表.get().getMapName(mapId)+"】");
					_mobList.put(mapId, mob[0]);
				}
				_allMap.addAll(_tempList);
				_tempList.clear();
				minus = 0 ;
				chance = Random.nextInt(40)+30;
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
			System.out.println("PC 移民處理時間軸異常重啟");
			GeneralThreadPool.getInstance().cancel(_timer, false);
			goblinThread.getInstance().start();			
		}
	}
	public void npcIsDead(final L1NpcInstance npc , final String attackName){
		final int mapId = npc.getMapId();
		_mobList.remove(mapId);
		L1World.getInstance().broadcastPacketToAll("\\aE【 "+特殊列表.get().getMapName(mapId)+"】的盜寶魔被降伏了。");
	}
	
}
